var events = require("events");

var EventEmitter = events.EventEmitter;

//定义构造函数
function Dog(name, energy){
    EventEmitter.call(this);
    this.name = name;
    this.energy = energy;

    /**
     * function函数，函数内this指向取决于调用主体
     * 箭头函数，函数内this指向函数定义的上下文
     */
    var intervalId = setInterval(() =>{
        if(this.energy >= 0){
            this.emit("bark");
            this.energy--;
        }else{
            clearInterval(intervalId);
        }
    }, 1000);
}

//继承prototype,遍历对象中的属性
for(var i in EventEmitter.prototype){
    Dog.prototype[i] = EventEmitter.prototype[i];
}

//对外公布Dog函数
module.exports = Dog;